/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.msk.base.service;



import com.msk.base.domain.goods.Goods;
import com.msk.base.domain.PriceCompare;
import com.msk.base.mapper.PriceCompareDao;
import com.msk.base.service.goods.GoodsService;
import com.msk.common.core.domain.model.LoginUser;
import com.msk.common.utils.DateUtils;
import com.msk.common.utils.spring.SpringUtils;
import com.msk.sys.domain.User;
import com.msk.sys.service.CrudService;
import com.msk.sys.utils.DictMskUtils;
import com.msk.wlhy.domain.*;
import com.msk.wlhy.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * @author llh
 * @date 2023/5/10
 * @return 
 * @description 推送价格表Service      
 */
@Service
@Transactional(readOnly = true)
public class PriceCompareService extends
		CrudService<PriceCompareDao, PriceCompare> {
	private GoodsService goodsService;
	
	private HtLogisticsOrderService htLogisticsOrderService;
	private HtDriverFileService htDriverFileService;
	private HtLogisticsOrderDetailService htLogisticsOrderDetailService;

	public PriceCompare get(String id) {
		return super.get(id);
	}

	public List<PriceCompare> findList(PriceCompare priceCompare) {
		return super.findList(priceCompare);
	}

	
	@Transactional(readOnly = false)
	public void save(PriceCompare priceCompare) {
	
		super.save(priceCompare);
	}
	
	/**
	 * @author llh
	 * @date 2023/5/10
	 * @return void
	 * @description 司机出价保存和更新使用       
	 */
	@Transactional(readOnly = false)
	public void priceCompareSave(PriceCompare priceCompare,LoginUser user) {
		htDriverFileService = SpringUtils.getBean(HtDriverFileService.class);
		HtSupplierService htSupplierService = SpringUtils.getBean(HtSupplierService.class);
		if(user!=null && "1".equals(user.getLoginObject())) {
			HtDriverFile htDriverFile = htDriverFileService.getDriverByUserId(user.getLoginId());
			priceCompare.setHtDriverFile(htDriverFile);
		}else if(user!=null && "3".equals(user.getLoginObject())){
			HtSupplier htSupplier = htSupplierService.getHtSupplierByUserId(user.getLoginId());
			priceCompare.setHtDriverFile(new HtDriverFile(htSupplier.getId()));
		}
		Double ratio = Double.parseDouble(DictMskUtils.getDictValue("coefficient","sys_config",""));
		Double driverPriceOffer =null;
		Double driverTatolOffer =null;
		Double officialPriceOffer =null;
		Double officialTatolOffer =null;
		System.out.println("报价方式："+priceCompare.getOfterType());
		if("0".equals(priceCompare.getOfterType())){
//			从单价算出其他价格
			driverPriceOffer = priceCompare.getDriverPriceOffer();
			officialPriceOffer = ratio*driverPriceOffer;
		}else if("1".equals(priceCompare.getOfterType())){
//		从总价算出其他价格
			driverTatolOffer = priceCompare.getDriverTatolOffer();
			officialTatolOffer = ratio*driverTatolOffer;
		}
		priceCompare.setDriverPriceOffer(driverPriceOffer);
		priceCompare.setOfficialPriceOffer(officialPriceOffer);
		priceCompare.setDriverTatolOffer(driverTatolOffer);
		priceCompare.setOfficialTatolOffer(officialTatolOffer);
		if (StringUtils.isBlank(priceCompare.getId())) {
			priceCompare.setOfferStatus("0");
		}
		PriceCompare param = new PriceCompare();
		param.setHtDriverFile(priceCompare.getHtDriverFile());
		param.setGoods(priceCompare.getGoods());
		List<PriceCompare> list = dao.findList(param);
		
	
		if(!list.isEmpty()){
			param = list.get(0);
				param.setDriverPriceOffer(priceCompare.getDriverPriceOffer());
				param.setOfficialPriceOffer(priceCompare.getOfficialPriceOffer());
				param.setDriverTatolOffer(priceCompare.getDriverTatolOffer());
				param.setOfficialTatolOffer(priceCompare.getOfficialTatolOffer());
			param.setOfterType(priceCompare.getOfterType());
			super.save(param);
			
		}else {
			
			super.save(priceCompare);
		}
	}

	@Transactional(readOnly = false)
	public void delete(PriceCompare priceCompare) {
		super.delete(priceCompare);
	}

	@Transactional(readOnly = false)
	public int deleteIds(String[] ids) {
		int result = 0;
		for (String id : ids) {
			super.delete(new PriceCompare(id));
			result++;
		}
		return result;
	}
	
	/**
	 * @author llh
	 * @date 2023/5/10
	 * @return void
	 * @description 下单使用       
	 */
	@Transactional(readOnly = false)
	public void downs(String goodsId, String[] selectIds) {
		htLogisticsOrderDetailService = SpringUtils.getBean(HtLogisticsOrderDetailService.class);
		htLogisticsOrderService = SpringUtils.getBean(HtLogisticsOrderService.class);
		goodsService = SpringUtils.getBean(GoodsService.class);

		Goods goods = goodsService.get(goodsId);
		goods.setType("11");
		goodsService.save(goods);
		// 通过goodsId查出所有比价数据	
		for (String id : selectIds) {
			PriceCompare priceCompare = this.get(id);
			priceCompare.setOfferStatus("3");			
			this.save(priceCompare);
			// 生成合同
			HtLogisticsOrder htLogisticsOrder = new HtLogisticsOrder();
			htLogisticsOrder.setCompany(goods.getCompany());
			htLogisticsOrder.setOffice(goods.getOffice());
			

			String htCode = DictMskUtils.getDictLabel("ZYH", "ht_code", "ZYH") + "-";

			String dateStr =  DateUtils.getDate("yyyyMMdd-");
			String orderNo = htLogisticsOrderService.getOrderNo();
			String orderNoStr = "";
			if(null != orderNo){
				orderNo = orderNo.replace(htCode,"");
				//判断今天是否已经有托运单
				if(orderNo.startsWith(dateStr)){
					//有递增+1
					orderNo = orderNo.replace(dateStr,"");
					Integer originalNumberInt = Integer.parseInt(orderNo) + 1;
					orderNoStr = htCode+ dateStr+ String.format("%05d", originalNumberInt);
				}else{
					//没有生成第一单
					orderNoStr = htCode+dateStr + "00001";
				}
			}else{
				//没有生成第一单
				orderNoStr = htCode+dateStr + "00001";
			}
			htLogisticsOrder.setOrderNo(orderNoStr);
			htLogisticsOrder.setCustomer(goods.getCustomer().getId());
			htLogisticsOrder.setGoodsType(goods.getGoodsType());
			htLogisticsOrder.setGoodsName(goods.getName());
			htLogisticsOrder.setConsigner(goods.getContact());
			htLogisticsOrder.setConsignee(goods.getContact1());
			htLogisticsOrder.setConsignerNumber(goods.getContactNumber());
			htLogisticsOrder.setConsigneeNumber(goods.getContactNumber1());

			//add by wlxiang 20201103
			htLogisticsOrder.setConsignerUser(goods.getContactUser());
			htLogisticsOrder.setConsignerUserTwo(goods.getContactUserTwo());
			htLogisticsOrder.setConsignerNumberTwo(goods.getContactNumberTwo());

			htLogisticsOrder.setConsigneeUser(goods.getContact1User());
			htLogisticsOrder.setConsigneeUserTwo(goods.getContactUser1Two());
			htLogisticsOrder.setConsigneeNumberTwo(goods.getContactNumber1Two());
			//add by wlxiang 20201103 end
			htLogisticsOrder.setStartArea(goods.getStartArea());
			htLogisticsOrder.setStartAreaName(goods.getStartAreaName());
			htLogisticsOrder.setEndArea(goods.getEndArea());
			htLogisticsOrder.setEndAreaName(goods.getEndAreaName());
			htLogisticsOrder.setAllGoodWeight(goods.getWeight());
			// 有疑问遍历报价设置合同总价只会把最后一个报价设为合同总价
			/*if(selectIds.length > 0){
				PriceCompare priceCompare = this.get(selectIds[0]);
				if("1".equals(priceCompare.getOfterType())){
					htLogisticsOrder.setAllFreight(priceCompare.getOfficialTatolOffer());
				}else{
					htLogisticsOrder.setAllFreight(priceCompare.getOfficialPriceOffer()*goods.getWeight());
				}
			}*/
			
			// 如果税率为空，则默认税率为6%
			String textRate1 = DictMskUtils.getDictValue("9","textrate_type","9");
			if(StringUtils.isNotBlank(textRate1)){
				goods.setTextRate(textRate1);
			}else{
				goods.setTextRate("9");
			}
					htLogisticsOrder.setTextRate(goods.getTextRate());
					
					BigDecimal goodsAllPrice;
					BigDecimal goodsPrice;
					// 总价为空的话通过单价计算出总价
					if("0".equals(priceCompare.getOfterType())){
						goodsAllPrice = new BigDecimal(priceCompare.getOfficialPriceOffer()*goods.getWeight()).setScale(3, RoundingMode.HALF_UP);
						goodsPrice = new BigDecimal(priceCompare.getOfficialPriceOffer()).setScale(3, RoundingMode.HALF_UP);
					}else{
						goodsAllPrice = new BigDecimal(priceCompare.getOfficialTatolOffer()).setScale(3, RoundingMode.HALF_UP);
						goodsPrice = new BigDecimal(priceCompare.getOfficialTatolOffer()/goods.getWeight()).setScale(3, RoundingMode.HALF_UP);
					}
					// 设置合同单价
					htLogisticsOrder.setTransportPrice(String.valueOf(goodsPrice.setScale(2, RoundingMode.HALF_UP).doubleValue()));
					// 设置含税金额
					htLogisticsOrder.setRevicePrice(String.valueOf(goodsAllPrice.setScale(2, RoundingMode.HALF_UP).doubleValue()));
					// 计算税额
					//BigDecimal rateMoney = goodsAllPrice.divide(new BigDecimal("100")).multiply(new BigDecimal(goods.getTextRate())).setScale(2, RoundingMode.HALF_UP);
					BigDecimal textRate = new BigDecimal(goods.getTextRate()).divide(new BigDecimal("100"));
					BigDecimal rateMoney = goodsAllPrice.divide(new BigDecimal("1").add(textRate)).multiply(textRate);
					// 设置税额
					htLogisticsOrder.setRateMoney(String.valueOf(rateMoney.setScale(2, RoundingMode.HALF_UP).doubleValue()));
					// 计算不含税金额
					/*BigDecimal outRateMoney = goodsAllPrice.subtract(rateMoney);*/
					//BigDecimal outRateMoney = goodsAllPrice.multiply(((new BigDecimal("100")).subtract(new BigDecimal(goods.getTextRate()))).divide(new BigDecimal("100"))).setScale(3, RoundingMode.HALF_UP);
					BigDecimal outRateMoney =goodsAllPrice.subtract(rateMoney);
					// 设置不含税金额
					htLogisticsOrder.setOutRateMoney(String.valueOf(outRateMoney.setScale(2, RoundingMode.HALF_UP).doubleValue()));
					// 设置合同总金额
					htLogisticsOrder.setAllFreight(goodsAllPrice.setScale(2, RoundingMode.HALF_UP).doubleValue());
					// 得到直接发布系数值
					String dictValue = DictMskUtils.getDictValue("once_coefficient", "sys_config", "");
					// 计算运输单价
					BigDecimal freight = goodsPrice.multiply(new BigDecimal(dictValue));
					
			htLogisticsOrder.setStatus("2");
			htLogisticsOrder.setGoods(goods);
			htLogisticsOrderService.save(htLogisticsOrder);
			
			
			//生成合同明细
			HtLogisticsOrderDetail htLogisticsOrderDetail = new HtLogisticsOrderDetail();
			htLogisticsOrderDetail.setCompany(goods.getCompany());
			htLogisticsOrderDetail.setOffice(goods.getOffice());
			htLogisticsOrderDetail.setOrderId(htLogisticsOrder.getId());
			htLogisticsOrderDetail.setGoodsName(goods.getName());
			htLogisticsOrderDetail.setGoodsWeight(goods.getWeight());
			htLogisticsOrderDetail.setGoodsType(goods.getGoodsType());
			htLogisticsOrderDetail.setConsigner(goods.getContact());
			htLogisticsOrderDetail.setConsignee(goods.getContact1());
			htLogisticsOrderDetail.setConsignerNumber(goods.getContactNumber());
			htLogisticsOrderDetail.setConsigneeNumber(goods.getContactNumber1());

			//add by wlxiang 20201103
			htLogisticsOrderDetail.setConsignerUser(goods.getContactUser());
			htLogisticsOrderDetail.setConsignerUserTwo(goods.getContactUserTwo());
			htLogisticsOrderDetail.setConsignerNumberTwo(goods.getContactNumberTwo());

			htLogisticsOrderDetail.setConsigneeUser(goods.getContact1User());
			htLogisticsOrderDetail.setConsigneeUserTwo(goods.getContactUser1Two());
			htLogisticsOrderDetail.setConsigneeNumberTwo(goods.getContactNumber1Two());
			//add by wlxiang 20201103 end

			htLogisticsOrderDetail.setLoadingArea(goods.getStartArea());
			htLogisticsOrderDetail.setLoadingAreaName(goods.getStartAreaName());
			htLogisticsOrderDetail.setLoadingPlace(goods.getStartPlace());
			htLogisticsOrderDetail.setUnloadingArea(goods.getEndArea());
			htLogisticsOrderDetail.setUnloadingAreaName(goods.getEndAreaName());
			htLogisticsOrderDetail.setUnloadingPlace(goods.getEndPlace());
			htLogisticsOrderDetail.setStartCoordinate(goods.getStartCoordinate());
			htLogisticsOrderDetail.setEndCoordinate(goods.getEndCoordinate());
			
			htLogisticsOrderDetail.setCustomer(goods.getCustomer().getId());
			htLogisticsOrderDetail.setStatus("1");
			htLogisticsOrderDetail.setAttstr03("1");
			
			// 设置运输单价
			htLogisticsOrderDetail.setFreight(freight.setScale(2, RoundingMode.HALF_UP).doubleValue());
			htLogisticsOrderDetail.setTextRate(goods.getTextRate());	
			//合同单价
			htLogisticsOrderDetail.setPrice(goodsPrice.setScale(2, RoundingMode.HALF_UP).doubleValue());
			//含税金额
			htLogisticsOrderDetail.setRevicePrice(String.valueOf(goodsAllPrice.setScale(2, RoundingMode.HALF_UP).doubleValue()));	
			// 设置税额
			htLogisticsOrderDetail.setRateMoney(String.valueOf(rateMoney.setScale(2, RoundingMode.HALF_UP).doubleValue()));
			// 设置不含税金额
			htLogisticsOrderDetail.setOutRateMoney(String.valueOf(outRateMoney.setScale(2, RoundingMode.HALF_UP).doubleValue()));
			
			if(StringUtils.isNoneEmpty(goods.getGoodsNums())){			
				htLogisticsOrderDetail.setGoodsNumber(Double.parseDouble(goods.getGoodsNums()));
			}
			
			htLogisticsOrderDetailService.save(htLogisticsOrderDetail);		
			
		}
	}
	

	/**
	 * 获取货源的最低报价信息
	 * @param priceCompare
	 * @return
	 */
	public PriceCompare getMinPrice(PriceCompare priceCompare){
		return dao.getMinimumPrice(priceCompare);
	}

	/**
	 * @author llh
	 * @date 2023/5/10
	 * @return void
	 * @description 取消订单      
	 */
	@Transactional(readOnly = false)
	public void cancleOrder(String goodsId) {
		//删除报价数据
		dao.deleteByGoodsId(goodsId);
		//将货源状态修改为草稿状态
		goodsService = SpringUtils.getBean(GoodsService.class);
		Goods g = goodsService.get(goodsId);
		g.setType("0");
		goodsService.save(g);
	}

	/**
	 * @author llh
	 * @date 2023/6/16
	 * @return void
	 * @description 货主签约使用      
	 */
	@Transactional(readOnly = false)
	public void downsOrder(String goodsId, String priceId) {
		htLogisticsOrderDetailService = SpringUtils.getBean(HtLogisticsOrderDetailService.class);
		htLogisticsOrderService = SpringUtils.getBean(HtLogisticsOrderService.class);
		goodsService = SpringUtils.getBean(GoodsService.class);
		HtLogisticsFrameService htLogisticsFrameService =  SpringUtils.getBean(HtLogisticsFrameService.class);
		Goods goods = goodsService.get(goodsId);
		goods.setType("11");
		goodsService.save(goods);
		//通过priceId查出所有比价数据
		PriceCompare priceCompare = this.get(priceId);
		priceCompare.setOfferStatus("3");
		this.save(priceCompare);
		//生成合同
		HtLogisticsOrder htLogisticsOrder = new HtLogisticsOrder();
		htLogisticsOrder.setCompany(goods.getCompany());
		htLogisticsOrder.setOffice(goods.getOffice());

		String htCode = DictMskUtils.getDictLabel("ZYH", "ht_code", "ZYH") + "-";

		String dateStr =  DateUtils.getDate("yyyyMMdd-");
		String orderNo = htLogisticsOrderService.getOrderNo();
		String orderNoStr = "";
		if(null != orderNo){
			orderNo = orderNo.replace(htCode,"");
			//判断今天是否已经有托运单
			if(orderNo.startsWith(dateStr)){
				//有递增+1
				orderNo = orderNo.replace(dateStr,"");
				Integer originalNumberInt = Integer.parseInt(orderNo) + 1;
				orderNoStr = htCode+ dateStr+ String.format("%05d", originalNumberInt);
			}else{
				//没有生成第一单
				orderNoStr = htCode+dateStr + "00001";
			}
		}else{
			//没有生成第一单
			orderNoStr = htCode+dateStr + "00001";
		}

		htLogisticsOrder.setOrderNo(orderNoStr);
		htLogisticsOrder.setCustomer(goods.getContact());
		htLogisticsOrder.setGoodsType(goods.getGoodsType());
		htLogisticsOrder.setGoodsName(goods.getName());
		htLogisticsOrder.setConsigner(goods.getContact());
		htLogisticsOrder.setConsignee(goods.getContact1());
		
		htLogisticsOrder.setConsignerUser(goods.getContactUser());
		htLogisticsOrder.setConsignerUserTwo(goods.getContactUserTwo());
		htLogisticsOrder.setConsignerNumberTwo(goods.getContactNumberTwo());

		htLogisticsOrder.setConsigneeUser(goods.getContact1User());
		htLogisticsOrder.setConsigneeUserTwo(goods.getContactUser1Two());
		htLogisticsOrder.setConsigneeNumberTwo(goods.getContactNumber1Two());
		
		htLogisticsOrder.setConsignerNumber(goods.getContactNumber());
		htLogisticsOrder.setConsigneeNumber(goods.getContactNumber1());
		htLogisticsOrder.setStartArea(goods.getStartArea());
		htLogisticsOrder.setStartAreaName(goods.getStartAreaName());
		htLogisticsOrder.setEndArea(goods.getEndArea());
		htLogisticsOrder.setEndAreaName(goods.getEndAreaName());
		htLogisticsOrder.setAllGoodWeight(goods.getWeight());
		htLogisticsOrder.setAttstr05(goods.getMeasureUnit());
		//记录车辆计价单位
		htLogisticsOrder.setPriceWay(goods.getPriceWay());

		String textRate1 = DictMskUtils.getDictValue("9","textrate_type","9");
		if(StringUtils.isNotBlank(textRate1)){
			goods.setTextRate(textRate1);
		}else{
			goods.setTextRate("9");
		}
		htLogisticsOrder.setTextRate(goods.getTextRate());

		BigDecimal goodsAllPrice;
		BigDecimal goodsPrice;
		String distanceStr = goods.getDistance();
		Double distance = Double.parseDouble(distanceStr);

		//合同单价
		goodsPrice = new BigDecimal(priceCompare.getOfficialPriceOffer()).setScale(8, RoundingMode.HALF_UP);
		//合同总价
		goodsAllPrice = new BigDecimal(priceCompare.getOfficialTatolOffer());
		//按不同报价方式计算合同总价或单价
		if ("2".equalsIgnoreCase(goods.getPriceWay())){
			//整车费用计算
			goodsPrice = new BigDecimal(priceCompare.getOfficialPriceOffer());
			goodsAllPrice = new BigDecimal(priceCompare.getOfficialPriceOffer() * Double.parseDouble(goods.getCarNum())).setScale(8, RoundingMode.DOWN);
			//货运数量
			htLogisticsOrder.setCarNum(goods.getCarNum());
		}else if ("3".equalsIgnoreCase(goods.getPriceWay())){
			if("0".equalsIgnoreCase(goods.getPriceType())){
				goodsAllPrice =  new BigDecimal(priceCompare.getOfficialPriceOffer()* goods.getWeight()).setScale(8, RoundingMode.DOWN);
			}else{
				goodsPrice = new BigDecimal(priceCompare.getOfficialTatolOffer()/goods.getWeight()).setScale(8, RoundingMode.DOWN);
			}
		}else{
			if("0".equalsIgnoreCase(goods.getPriceType())){
				goodsAllPrice =  new BigDecimal(priceCompare.getOfficialPriceOffer()* goods.getWeight()*distance).setScale(8, RoundingMode.DOWN);
			}else{
				goodsPrice = new BigDecimal(priceCompare.getOfficialTatolOffer()/goods.getWeight()/distance).setScale(8, RoundingMode.DOWN);
			}
		}

		// 设置运输单价
		htLogisticsOrder.setTransportPrice(String.valueOf(goodsPrice.doubleValue()));
		// 设置含税金额
		htLogisticsOrder.setRevicePrice(String.valueOf(goodsAllPrice.doubleValue()));
		// 计算税额
		BigDecimal textRate = new BigDecimal(goods.getTextRate()).divide(new BigDecimal("100"));
		BigDecimal textRateTemp = new BigDecimal("1").add(textRate);
		BigDecimal rateMoney = goodsAllPrice.divide(textRateTemp,8,RoundingMode.HALF_UP).multiply(textRate).setScale(2,RoundingMode.HALF_UP);
		// 设置税额
		htLogisticsOrder.setRateMoney(String.valueOf(rateMoney.doubleValue()));
		// 计算不含税金额
		BigDecimal outRateMoney =goodsAllPrice.subtract(rateMoney).setScale(2,RoundingMode.HALF_UP);
		// 设置不含税金额
		htLogisticsOrder.setOutRateMoney(String.valueOf(outRateMoney.doubleValue()));
		// 设置合同总金额
		htLogisticsOrder.setAllFreight(goodsAllPrice.doubleValue());


		htLogisticsOrder.setStatus("2");
		htLogisticsOrder.setGoods(goods);
		htLogisticsOrderService.save(htLogisticsOrder);


		//生成合同明细
		HtLogisticsOrderDetail htLogisticsOrderDetail = new HtLogisticsOrderDetail();


		String htCodeDetail = DictMskUtils.getDictLabel("TYD", "ht_code", "TYD") + "-";
		//获取当前日期
		htLogisticsOrderDetail.setOriginalNumber(htCodeDetail);
		String originalNumber = htLogisticsOrderDetailService.getOriginalNumber(htLogisticsOrderDetail );
		String originalNumberStr = "";
		if(null != originalNumber){
			originalNumber = originalNumber.replace(htCodeDetail,"");
			//判断今天是否已经有托运单
			if(originalNumber.startsWith(dateStr)){
				//有递增+1
				originalNumber = originalNumber.replace(dateStr,"");
				Integer originalNumberInt = Integer.parseInt(originalNumber) + 1;
				originalNumberStr = htCodeDetail+ dateStr+ String.format("%05d", originalNumberInt);
			}else{
				//没有生成第一单
				originalNumberStr = htCodeDetail+dateStr + "00001";
			}
		}else{
			//没有生成第一单
			originalNumberStr = htCodeDetail+dateStr + "00001";
		}

		htLogisticsOrderDetail.setOriginalNumber(originalNumberStr);
		String supplierId = priceCompare.getHtDriverFile().getId();
		//承运商
		htLogisticsOrderDetail.setSupplier(new HtSupplier(supplierId));
		if(null != goods.getHtLogisticsFrame()){
			//部分货主可能没有合同
			HtLogisticsFrame htLogisticsFrame = htLogisticsFrameService.get(goods.getHtLogisticsFrame().getId());
			//托运人信息
			htLogisticsOrderDetail.setConsignUser(new HtCompanyCustomer(htLogisticsFrame.getConsignUser()));
		}


		htLogisticsOrderDetail.setOrderType("1");
		htLogisticsOrderDetail.setValidNum(goods.getValidNum()==null? null:goods.getValidNum().toString());
		htLogisticsOrderDetail.setDistance(goods.getDistance());
		htLogisticsOrderDetail.setMeasureUnit(goods.getMeasureUnit());
		htLogisticsOrderDetail.setAttstr05(goods.getMeasureUnit());
		htLogisticsOrderDetail.setGenerateStatus("0");
		htLogisticsOrderDetail.setPriceUnit(goods.getPriceUnit());
		htLogisticsOrderDetail.setGoodsStartTime(goods.getPickTime());
		htLogisticsOrderDetail.setGoodsEndTime(goods.getDeliveryTime());

		htLogisticsOrderDetail.setCompany(goods.getCompany());
		htLogisticsOrderDetail.setOffice(goods.getOffice());
		htLogisticsOrderDetail.setOrderId(htLogisticsOrder.getId());
		htLogisticsOrderDetail.setGoodsName(goods.getName());
		htLogisticsOrderDetail.setGoodsWeight(goods.getWeight());
		htLogisticsOrderDetail.setGoodsType(goods.getGoodsType());
		htLogisticsOrderDetail.setConsigner(goods.getContact());
		htLogisticsOrderDetail.setConsignee(goods.getContact1());
		htLogisticsOrderDetail.setConsignerNumber(goods.getContactNumber());
		htLogisticsOrderDetail.setConsigneeNumber(goods.getContactNumber1());
		
		htLogisticsOrderDetail.setConsignerUser(goods.getContactUser());
		htLogisticsOrderDetail.setConsignerUserTwo(goods.getContactUserTwo());
		htLogisticsOrderDetail.setConsignerNumberTwo(goods.getContactNumberTwo());

		htLogisticsOrderDetail.setConsigneeUser(goods.getContact1User());
		htLogisticsOrderDetail.setConsigneeUserTwo(goods.getContactUser1Two());
		htLogisticsOrderDetail.setConsigneeNumberTwo(goods.getContactNumber1Two());

		htLogisticsOrderDetail.setLoadingArea(goods.getStartArea());
		htLogisticsOrderDetail.setLoadingAreaName(goods.getStartAreaName());
		htLogisticsOrderDetail.setLoadingPlace(goods.getStartPlace());
		htLogisticsOrderDetail.setUnloadingArea(goods.getEndArea());
		htLogisticsOrderDetail.setUnloadingAreaName(goods.getEndAreaName());
		htLogisticsOrderDetail.setUnloadingPlace(goods.getEndPlace());
		htLogisticsOrderDetail.setStartCoordinate(goods.getStartCoordinate());
		htLogisticsOrderDetail.setEndCoordinate(goods.getEndCoordinate());

		htLogisticsOrderDetail.setCustomer(goods.getContact());
		htLogisticsOrderDetail.setStatus("1");
		htLogisticsOrderDetail.setAttstr03("1");



		BigDecimal freightLogistics;
		BigDecimal allFreightLogistics;
		//总价为空的话通过单价计算出总价
		//修改运费金额取承运商报价的金额，不是平台推送价格

		if ("2".equalsIgnoreCase(goods.getPriceWay())){
			//整车费用计算
			freightLogistics = new BigDecimal(priceCompare.getOfficialPriceOffer());
			allFreightLogistics = new BigDecimal(priceCompare.getOfficialPriceOffer() * Double.parseDouble(goods.getCarNum())).setScale(8, RoundingMode.DOWN);
		}else{
			if("0".equals(priceCompare.getOfterType())){
				allFreightLogistics = new BigDecimal(priceCompare.getDriverPriceOffer()*goods.getWeight()*distance).setScale(8, RoundingMode.DOWN);
				freightLogistics = new BigDecimal(priceCompare.getDriverPriceOffer()).setScale(8, RoundingMode.DOWN);
			}else{
				allFreightLogistics = new BigDecimal(priceCompare.getDriverTatolOffer()).setScale(8, RoundingMode.DOWN);
				freightLogistics = new BigDecimal(priceCompare.getDriverTatolOffer()/goods.getWeight()/distance).setScale(8, RoundingMode.DOWN);
			}
		}

		// 得到直接发布系数值
		String dictValue = DictMskUtils.getDictValue("once_coefficient", "sys_config", "");
		// 计算运输单价
		BigDecimal freight = freightLogistics.multiply(new BigDecimal(dictValue));

		// 设置运输单价
		htLogisticsOrderDetail.setFreight(freight.setScale(8, RoundingMode.DOWN).doubleValue());
		htLogisticsOrderDetail.setTextRate(goods.getTextRate());
		// 计算税额
		BigDecimal detailRateMoney = allFreightLogistics.divide(textRateTemp,8,RoundingMode.HALF_UP).multiply(textRate).setScale(2,RoundingMode.HALF_UP);

		// 计算不含税金额
		BigDecimal detailOutRateMoney =allFreightLogistics.subtract(detailRateMoney).setScale(2,RoundingMode.HALF_UP);

		htLogisticsOrderDetail.setPrice(freightLogistics.doubleValue());
		//含税金额
		htLogisticsOrderDetail.setRevicePrice(String.valueOf(allFreightLogistics));
		// 设置税额
		htLogisticsOrderDetail.setRateMoney(String.valueOf(detailRateMoney.setScale(2, RoundingMode.HALF_UP).doubleValue()));
		// 设置不含税金额
		htLogisticsOrderDetail.setOutRateMoney(String.valueOf(detailOutRateMoney.setScale(2, RoundingMode.HALF_UP).doubleValue()));

		if(StringUtils.isNoneEmpty(goods.getGoodsNums())){
			htLogisticsOrderDetail.setGoodsNumber(Double.parseDouble(goods.getGoodsNums()));
		}

		htLogisticsOrderDetailService.save(htLogisticsOrderDetail);
		final String id = htLogisticsOrder.getId();
		final String detailId = htLogisticsOrderDetail.getId();
		TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
			// 在事务提交之后执行的代码块（方法）  此处使用TransactionSynchronizationAdapter，其实在Spring5后直接使用接口也很方便了~
			@Override
			public void afterCommit() {
				//异步执行生成合同线程
				GenerateContratctThread generateContratctThread = new GenerateContratctThread(id, detailId);
				generateContratctThread.start();
			}
		});
	}

}